package system;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;

import system.MainFrame;

import connect.connect;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Login extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField UserName;
	private JPasswordField Password;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public int passed(String username, String password) {

		Connection conn = null;

		String sql = "SELECT * FROM Users WHERE UserName='"
				+ username.trim() + "'";
		try {

			conn = connect.getConnection();
			java.sql.Statement stat = conn.createStatement();

			ResultSet rs = ((java.sql.Statement) stat)
					.executeQuery(sql);
			while(rs.next()){
				if(password.trim().equals(rs.getString(2).trim())){
					return 2;
				}
				return 1;
			}
			return 0;
		} catch (Exception e) {
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return 0;
	}
	
	public Login() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(480, 200, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		final JLabel warning1 = new JLabel("");
		warning1.setForeground(Color.RED);

		final JLabel warning2 = new JLabel("");
		warning2.setForeground(Color.RED);
		
		UserName = new JTextField();
		UserName.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				Connection conn = null;
				String sql = "SELECT * FROM Users WHERE UserName="
						+ UserName.getText() + "";
				try {

					conn = connect.getConnection();
					java.sql.Statement stat = conn.createStatement();

					ResultSet rs = ((java.sql.Statement) stat)
							.executeQuery(sql);
					if(!rs.next()){
						warning2.setText("");
						warning1.setText("*用户名不存在！");
						
						
					}
					else{warning1.setText("");}
					
				} catch (Exception e) {
				} 
				
				
			}
		});
		UserName.setColumns(10);

		Password = new JPasswordField();
		Password.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent ke) {
				if(ke.getKeyCode()==KeyEvent.VK_ENTER){
					if ((UserName.getText() == null || "".equals(UserName.getText()))
							&& (Password.getText() == null || "".equals(Password
									.getText()))) {
						warning1.setText("*用户名不能为空");
						warning2.setText("*密码不能为空");
					} else if (UserName.getText() == null
							|| "".equals(UserName.getText())) {
						warning1.setText("*用户名不能为空");
						warning2.setText("");
					} else if (Password.getText() == null
							|| "".equals(Password.getText())) {
						warning1.setText("");
						warning2.setText("*密码不能为空");
					} else if (passed(UserName.getText(), Password.getText()) == 0) {
						UserName.setText(null);
						Password.setText(null);
						warning1.setText("*用户名错误");
					} else if (passed(UserName.getText(), Password.getText()) == 1) {
						Password.setText(null);
						warning1.setText("");
						warning2.setText("*密码错误");
					}
	 
					else {
						dispose();
						MainFrame frame = new MainFrame();
						frame.setVisible(true);
					    Constant.userName = UserName.getText();//获取用户名
					    
					}
				}
				
			}
		});
		Password.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent arg0) {

				
				
			}
		});
		Password.setEchoChar('*');

		JLabel label = new JLabel("\u7528\u6237\u540D\uFF1A");

		JLabel label_1 = new JLabel("\u5BC6  \u7801\uFF1A");

		

		JButton button = new JButton("\u767B\u9646");
		button.addMouseListener(new MouseAdapter() {
			@SuppressWarnings("deprecation")
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if ((UserName.getText() == null || "".equals(UserName.getText()))
						&& (Password.getText() == null || "".equals(Password
								.getText()))) {
					warning1.setText("*用户名不能为空");
					warning2.setText("*密码不能为空");
				} else if (UserName.getText() == null
						|| "".equals(UserName.getText())) {
					warning1.setText("*用户名不能为空");
					warning2.setText("");
				} else if (Password.getText() == null
						|| "".equals(Password.getText())) {
					warning1.setText("");
					warning2.setText("*密码不能为空");
				} else if (passed(UserName.getText(), Password.getText()) == 0) {
					UserName.setText(null);
					Password.setText(null);
					warning1.setText("*用户名错误");
				} else if (passed(UserName.getText(), Password.getText()) == 1) {
					Password.setText(null);
					warning1.setText("");
					warning2.setText("*密码错误");
				}
 
				else {
					dispose();
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				    Constant.userName = UserName.getText();//获取用户名
				    
				}

			}

			

			

		});

		JButton button_1 = new JButton("\u9000\u51FA");
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				dispose();
			}
		});

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(114)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(label_1)
								.addComponent(label))
							.addGap(21)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(Password, Alignment.LEADING)
									.addComponent(UserName, Alignment.LEADING))
								.addComponent(button_1))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(warning1)
								.addComponent(warning2)))
						.addComponent(button))
					.addGap(151))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(67)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(UserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label)
						.addComponent(warning1))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1)
						.addComponent(warning2))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(button)
						.addComponent(button_1))
					.addContainerGap(84, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
